Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing dismatch between dense matrix() and sparse repr sparse_matrix() of GlobalPhase #6940

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

JerryChen97
Copy link
Contributor

Context:
qml.GlobalPhase does not set its hyperparameters during initialization. This caused certain bugs, e.g.

>>> np.allclose(qml.GlobalPhase(0.123).matrix(), qml.GlobalPhase(0.123).sparse_matrix().toarray())
False
>>> qml.GlobalPhase(0.123).matrix().shape
(1, 1)
>>> qml.GlobalPhase(0.123).sparse_matrix().shape
(2, 2)
>>> qml.GlobalPhase(0.123).matrix()
array([[0.99244503-0.12269009j]])
>>> qml.GlobalPhase(0.123).sparse_matrix().toarray()
array([[0.99244503-0.12269009j, 0.        +0.j        ],
       [0.        +0.j        , 0.99244503-0.12269009j]])

where GlobalPhase.sparse_matrix() did the calculation wrongly because it never received correct n_wires from hyperparameters, which is required by the base class Operator

Description of the Change:

Benefits:

Possible Drawbacks:

Related GitHub Issues:

@JerryChen97 JerryChen97 marked this pull request as ready for review February 7, 2025 21:15
@JerryChen97 JerryChen97 self-assigned this Feb 7, 2025
Copy link
Contributor

github-actions bot commented Feb 7, 2025

Hello. You may have forgotten to update the changelog!
Please edit doc/releases/changelog-dev.md with:

  • A one-to-two sentence description of the change. You may include a small working example for new features.
  • A link back to this PR.
  • Your name (or GitHub username) in the contributors section.

pennylane/ops/identity.py Outdated Show resolved Hide resolved
pennylane/ops/identity.py Outdated Show resolved Hide resolved
@JerryChen97 JerryChen97 changed the title Fixing missing hyperparameters of GlobalPhase Fixing dismatch between dense matrix() and sparse repr sparse_matrix() of GlobalPhase Feb 10, 2025
Copy link

codecov bot commented Feb 10, 2025

Codecov Report

Attention: Patch coverage is 87.50000% with 1 line in your changes missing coverage. Please review.

Project coverage is 99.59%. Comparing base (a7514b7) to head (5c045e6).

Files with missing lines Patch % Lines
pennylane/math/matrix_manipulation.py 80.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6940      +/-   ##
==========================================
- Coverage   99.59%   99.59%   -0.01%     
==========================================
  Files         480      480              
  Lines       45510    45516       +6     
==========================================
+ Hits        45325    45330       +5     
- Misses        185      186       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

# global phase
wires = wire_order[0:1]
if qml.math.shape(mat) == (qudit_dim, qudit_dim):
wires = wire_order[0:1]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that the line119 has never been visited by the whole codebase. This means it actually only serves for the previous wrong impelmentation of GlobalPhase(.., wires=None). So we will just delete this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants